<!--   
Persistent Digital Archives and Library System (PeDALS) 
(c) 2008 - All Rights Reserved
//-->
<?php
if ((is_file('includes\config.php')) and (is_file('includes\header.php')) and (is_file('includes\footer.php')) and (is_file('includes\style.css'))) {
	include 'includes\config.php';
} else {
	echo "Unable to locate one or all of the include files.  Please contact your PeDALS Administrator.";
	die;
}
	$term = urldecode(@$_GET["qry"]);	
	//submit FORM 
	if (isset($_POST['EditIngested'])){	

					$arrayId = $_POST["SeriesAcquisitionId"];
					$arrayValue = $_POST["SeriesAcquisitionIngested"];	
					$arrayVar = array_combine($arrayId,$arrayValue);		
				
					$tsql_callSP = "{call PAC_UpdateIngested( ?, ?)}";
					
				//edit existing variant names
				$i = 0;	
				if (!empty($arrayVar)){	
					while ($i < count($arrayId)) {
						$varId = $arrayId[$i];
						$varValue = $arrayValue[$i];
							$params = array(   								 
								 array($varId, SQLSRV_PARAM_IN),  
								 array($varValue, SQLSRV_PARAM_IN) 
							   ); 
							$stmt = sqlsrv_query( $conn, $tsql_callSP, $params);	
									   
						   if (!$stmt){
								$SQLmessageError = "One or more ingested values were not updated due to an error.";
								$SQLErrorDB = print_r(sqlsrv_errors(),true);
								$SQLErrorDBcheck = 1;
								sqlsrv_close($conn);
								break;
								}	
						 $i++;
						} 
				}
			if (!isset($SQLErrorDBcheck)){
				include("includes/addIngSuccess.php");
				sqlsrv_close( $conn);
				exit;
			}	
	
	}
	
	
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta name="description" content="Persistent Digital Archives and Library System" />
    <title>PeDALS Web Administration: Acquisitions by Status</title>
    <link rel="shortcut icon" href="images/favicon.ico" />
    <link href="includes/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php include 'includes\header.php'; ?>
<div id="breadcrumb-container">
  <div id="breadcrumb"><a href="index.php" title="Home page">Home</a> / <a href="acqSummary.php" title="Acquisitions">Acquisitions</a> / Acquisitions by Status</div>
  <div id="breadcrumb-side"><form name="search" action="search.php"><input type="text" name="q" width="50"/><input type="submit" value="Go" /></form><a href="advSearch.php" title="Advanced Search" alt="Advanced Search">Advanced Search</a>
  </div>
  <div class="clear"></div>
</div>
<!-- 
  CONTENT
  -->
<div id="content-container">
  <!-- 
	SIDE COLUMN
	-->
  <div id="content-side">
    <ul class="link-list-vertical">
      <li><a href="acqNew.php" title="Add a new acquisition to PeDALS.">Add Acquisition</a></li>
    </ul>
  </div>
  <!-- 
	MAIN COLUMN
	-->
  <div id="content"><a name="maincontent" id="maincontent"></a>
    <h1>Acquisitions by Ingest Status</h1>
    <p>Show:&nbsp;&nbsp;&nbsp;<a href="acqSummaryStatus.php">All</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="acqSummaryStatus.php?qry=rev">Ready for review</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="acqSummaryStatus.php?qry=sup">Superpackage has been created</a></p>
<form method="POST">  
<?php	
	if ($term == "rev") {	
		$tsql = "SELECT     Provenance.ProvenanceName, Series.SeriesTitle, SeriesAcquisition.SeriesAcquisitionId, CONVERT(VARCHAR, 
						  SeriesAcquisition.SeriesAcquisitionIngestDate, 101) AS Expr1, SeriesAcquisition.SeriesAcquisitionReadyToIngest, 
						  SeriesAcquisition.SeriesAcquisitionIngested, SeriesAcquisition.SeriesAcquisitionAccessionNumber, SeriesAcquisition.SuperPackageId
	FROM         SeriesAcquisition INNER JOIN
						  Series ON SeriesAcquisition.SeriesId = Series.SeriesId INNER JOIN
						  Provenance ON Series.ProvenanceId = Provenance.ProvenanceId
	WHERE     (SeriesAcquisition.SeriesAcquisitionReadyToIngest = 0) OR
						  (SeriesAcquisition.SeriesAcquisitionReadyToIngest = 3)
	ORDER BY Provenance.ProvenanceName, Series.SeriesTitle, SeriesAcquisition.SeriesAcquisitionIngestDate";
	} elseif ($term == "sup") {
		$tsql = "SELECT     Provenance.ProvenanceName, Series.SeriesTitle, SeriesAcquisition.SeriesAcquisitionId, CONVERT(VARCHAR, 
						  SeriesAcquisition.SeriesAcquisitionIngestDate, 101) AS Expr1, SeriesAcquisition.SeriesAcquisitionReadyToIngest, 
						  SeriesAcquisition.SeriesAcquisitionIngested, SeriesAcquisition.SeriesAcquisitionAccessionNumber, SeriesAcquisition.SuperPackageId
	FROM         SeriesAcquisition INNER JOIN
						  Series ON SeriesAcquisition.SeriesId = Series.SeriesId INNER JOIN
						  Provenance ON Series.ProvenanceId = Provenance.ProvenanceId
	WHERE     (SeriesAcquisition.SeriesAcquisitionReadyToIngest = 4)
	ORDER BY Provenance.ProvenanceName, Series.SeriesTitle, SeriesAcquisition.SeriesAcquisitionIngestDate";
	} else {
		$tsql = "SELECT Provenance.ProvenanceName, Series.SeriesTitle, SeriesAcquisition.SeriesAcquisitionId, CONVERT(VARCHAR, 
						  SeriesAcquisition.SeriesAcquisitionIngestDate, 101) AS Expr1, SeriesAcquisition.SeriesAcquisitionReadyToIngest, 
						  SeriesAcquisition.SeriesAcquisitionIngested, SeriesAcquisition.SeriesAcquisitionAccessionNumber, SeriesAcquisition.SuperPackageId
				FROM SeriesAcquisition INNER JOIN Series ON SeriesAcquisition.SeriesId = Series.SeriesId INNER JOIN Provenance ON Series.ProvenanceId = Provenance.ProvenanceId ORDER BY Provenance.ProvenanceName, Series.SeriesTitle, SeriesAcquisition.SeriesAcquisitionIngestDate";
	}

 	/* Create the statement  */
	$getData = sqlsrv_prepare( $conn, $tsql);
	if( $getData === false ) {
		 echo "Error in preparing statement. Please contact your PeDALS Administrator.";
		 die;
	}

	/* Execute the statement */
	sqlsrv_execute( $getData);
	
    if ($term == "rev") {	
	echo "<h2>The following acquisitions are ready for review:</h2>";
		} elseif ($term == "sup") {
	echo "<h2>Superpackages have been created for the following acquistions:</h2>";	
	} else {
	
	}
    echo "<table class=\"table\" border=\"1\" cellspacing=\"0\" summary=\"Table listing all the acquisitions set up in PeDALS\">";
	echo "<thead><tr>";
	echo "<th scope=\"col\">Provenance</th>";
	echo "<th scope=\"col\">Series</th>";
    echo "<th scope=\"col\">Submitted to PeDALS</th>";	
    echo "<th scope=\"col\">Ingest Status</th>";	
    echo "<th scope=\"col\">LOCKSS Status</th>";	
    echo "</tr></thead><tbody>";

	/* Start counting rows in the result set */
	$rowCount = 0;

	/* Fetch the data */
	while (sqlsrv_fetch( $getData ))
	{	
		$provName = sqlsrv_get_field($getData, 0);
		$serTitle = sqlsrv_get_field($getData, 1);	
		$acqId = sqlsrv_get_field($getData, 2);				
		$acqIngestDate = sqlsrv_get_field($getData, 3);	
		$readyIngest = sqlsrv_get_field($getData, 4);
		$ingested = sqlsrv_get_field($getData, 5);
		$accNumber = sqlsrv_get_field($getData, 6);
		$superpackageid = sqlsrv_get_field($getData, 7);

	
		echo "<tr>";
		echo "<td align=\"left\" valign=\"top\" >".$provName."</td>";				
		echo "<td align=\"left\" valign=\"top\" >".$serTitle."</td>";				
		echo "<td align=\"center\" valign=\"top\" >";
		echo "<a title=\"".$acqIngestDate."\"";
		echo " alt=\"".$acqIngestDate."\"";
		echo " href=acqDetail.php?acq=".$acqId."";
		echo ">".$acqIngestDate."</a>";
		echo "</td>";				
		echo "<td align=\"left\" valign=\"top\" >";
		switch ($readyIngest) {
			case 0:
				echo "Waiting for submission.";
				break;
			case 1:
				echo "Accepted. BizTalk creating superpackage.";	
				break;
			case 2:
				echo "Rejected. BizTalk removing records.";	
				break;
			case 3:
				echo "Rejected at least once. Waiting for resubmission. Items can be <a href=\"http://aslaprpedmanifest.lib.az.us/admin/itemSummary.php?acq=".$acqId."\">reviewed.</a>";
				break;		
			case 4:
				echo "Superpackage has been created.";
				break;
			case 5:
				echo "Ready for <a href=\"http://aslaprpedmanifest.lib.az.us/admin/itemSummary.php?acq=".$acqId."\">review.</a>";
				break;
			case 9:
				echo "BizTalk processing submitted records.";
				break;
		}
		echo "</td>";
		echo "<td align=\"left\" valign=\"top\" nowrap=\"nowrap\">";
		echo "<input type=\"hidden\" name=\"SeriesAcquisitionId[]\" value=\"".$acqId."\">";
		if ($readyIngest == 4) {
			echo "<input type=\"text\" name=\"SeriesAcquisitionIngested[]\" size=\"8\" maxlength=\"12\" value=\"".$ingested."\">";	
			echo "<input type=\"image\" name=\"EditIngested[]\" src=\"images/save.jpg\" border=\"0\" alt=\"Save all changes to this page\" title=\"Save all changes to this page\">";					
		} else {
			echo "<input type=\"hidden\" name=\"SeriesAcquisitionIngested[]\" value=\"".$ingested."\">";	
		}
		echo "</td>";			
		echo "</tr>";	
		$rowCount++;
		
    }	
	
	if( $rowCount == 0 ) { 
	    echo "<tr><td colspan=\"5\"><h4>There are no acquisitions.</h4></td></tr>";
    	}
		
	echo "</tbody></table>";		
/*	    sqlsrv_free_stmt( $getData );	*/
	sqlsrv_close( $conn);
	?>
<!--<input type="submit" value="submit changes" name="EditIngested">-->
</form>
<!--Perform the following if user clicks on the submit button-->
<?php


	include 'includes\footer.php'; 
	
	?>
  </div>
</div>
</body>
</html>